const http = require('http');

const app = http.createServer((req, res) => {
    // 网站图标请求，忽略掉
    if (req.url === '/favicon.ico') return;

    // 查看 cookie
    if (req.url === '/cookie') {
        console.log(req.headers.cookie);
        res.end();
        return;
    }

    // 设置 cookie
    if (req.url === '/setCookie') {
        res.setHeader('set-cookie', 'uid=1');
        res.end();
        return;
    }

    // 设置随机 cookie
    if (req.url === '/setCookieRandom') {
        res.setHeader(
            'Set-Cookie',
            `${String(Math.random()).substr(2, 10)}=${String(
                Math.random()
            ).substr(2, 10)}`
        );
    }

    // 其他请求不处理
    res.end('404 Not Found');
});

app.listen(3000);
